package cn.com.klun.tjy.dao.impl;

import cn.com.klun.tjy.dao.MonitorDAO;
import cn.com.klun.util.BasisConstants;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/** 
 * @ClassName: MonitorDAO 
 * @Description: 查询系统配置表信息
 * @author hzh 
 * @date 2016年6月30日 下午6:30:00  
 */
@Service
public class MonitorDAOImpl implements MonitorDAO {
	
	/**
	* @Title: getConn 
	* @Description: 获取数据库连接
	* @return Connection    返回类型 
	* @throws
	 */
	private Connection getConn(){		
		Connection conn = null;
		try {
			
			Class.forName(BasisConstants.driverClassName).newInstance();
			conn = DriverManager.getConnection(BasisConstants.driverUrl,BasisConstants.account,BasisConstants.password); 
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return conn;
	}

	/** 
	* @Title: getMonitorSendCount 
	* @Description: 统计无限制下发量数据
	* @return Map<String,String>    返回类型 
	* @throws
	 */
	@Override
	public Map<String, String> getMonitorSendCount(String sql) {		
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		Map<String,String> map=null;
		
		try {
			connection = getConn();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			map=new HashMap<String,String>();
			if(rs!=null&&rs.next())
			{
				map.put("giveNum", rs.getString(1));
				map.put("couponMoney", rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}

	/** 
	* @Title: getMonitorProfit 
	* @Description: 统计利润率数据
	* @return Map<String,String>    返回类型 
	* @throws
	 */
	@Override
	public Map<String, String> getMonitorProfit(String sql) {
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		Map<String,String> map=null;
		
		try {
			connection = getConn();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			map=new HashMap<String,String>();
			if(rs!=null&&rs.next())
			{
				map.put("giveNum", rs.getString(1));
				map.put("usedMoney", rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}

}
